local ma__xingyi = fk.CreateSkill {
  name = "ma__xingyi",
  tags = { Skill.Compulsory, },
}

Fk:loadTranslationTable {
  ["ma__xingyi"] = "星移",
  [":ma__xingyi"] = "锁定技，当你使用牌时，本回合牌堆顶的三张牌对你可见，然后你将牌堆中的第三张牌置于牌堆顶。",

  ["@$ma__xingyi-turn"] = "星移"
}

ma__xingyi:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(ma__xingyi.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "ma__xingyi-turn")
    if player.dead then return end
    local ids = {}
    if #room.draw_pile > 2 then
        table.insert(ids, room.draw_pile[3])
    end
      if #ids == 1 then
        room:moveCards({
          ids = ids,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonPut,
          skillName = ma__xingyi.name,
        })
      end
  end,
})

ma__xingyi:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ma__xingyi.name) or player:getMark("ma__xingyi-turn") == 0 then return false end
    for _, move in ipairs(data) do
      if move.toArea == Card.DrawPile then
        return true
      end

      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DrawPile then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ex_ids = player.room:getNCards(3)
    if ex_ids and player:hasSkill("ma__xingyi", true) then
      room:setPlayerMark(player, "@$ma__xingyi-turn", 0)
      table.forEach(ex_ids, function(id)
        local t = player:getTableMark("@$ma__xingyi-turn")
        table.insert(t, id)
        player:setMark("@$ma__xingyi-turn", t)
        room:doBroadcastNotify("SetPlayerMark", json.encode {
          player.id,
          "@$ma__xingyi-turn",
          t
        }, { player })
      end)

    end
  end,
})

return ma__xingyi
